package com.wwg.warehouse.data

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.sqlite.db.SupportSQLiteDatabase
import com.wwg.warehouse.data.dao.BookmarkDao
import splitties.init.appCtx
import java.util.*

val whDb by lazy {
    WarehouseDatabase.createDB(appCtx)
}

@Database(
    version = 1,
    exportSchema = true,
    entities = [DBEntity::class]
)
abstract class WarehouseDatabase: RoomDatabase() {

    abstract val bookmarkDao: BookmarkDao

    companion object {
        private const val DB_NAME = "bookmark.db"

        fun createDB(context: Context) =
            Room.databaseBuilder(context, WarehouseDatabase::class.java, DB_NAME)
                .addCallback(dbCb)
                .allowMainThreadQueries()
                .build()

        private val dbCb = object : Callback() {
            override fun onCreate(db: SupportSQLiteDatabase) {
                db.setLocale(Locale.CHINESE)
            }
        }
    }
}